<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="./bootstrap/css/bootstrap.min.css" />
  </head>

  <body>
    <div id="app">
      <button
        class="btn btn-outline-secondary btn-sm"
        @click="()=>filterList(1)"
        v-bind:class="{active:(activeStatus===1)}"
      >
        未完成
      </button>
      <button
        class="btn btn-outline-warning btn-sm"
        :class="{active:(activeStatus===2)}"
        @click="()=>filterList(2)"
      >
        进行中
      </button>
      <button
        class="btn btn-outline-primary btn-sm"
        :class="{active:(activeStatus===3)}"
        @click="()=>filterList(3)"
      >
        已完成
      </button>
      <table class="table">
        <thead>
          <tr>
            <th>名称</th>
            <th>状态</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="item in listArr" :key="item.id">
            <td>{{item.title}}</td>
            <td>{{status[item.status]}}</td>
          </tr>
        </tbody>
      </table>
    </div>
    <script type="module">
      import { createApp, ref, reactive, computed } from "./vue.esm-browser.js";
      import PocketBase from "./pocketbase.es.js";
      const pb = new PocketBase("http://127.0.0.1:8090");
      const app = createApp({
        setup() {
          const listArr = ref([]);
          const status = {
            1: "未完成",
            2: "进行中",
            3: "已完成",
          };
          // 按钮点击，设置状态
          const activeStatus = ref(1);
          function filterList(status) {
            activeStatus.value = status;
            getList();
          }

          // ------------

          async function getList() {
            const records = await pb
              .collection("todolist")
              .getList(1, 50, { filter: `status='${activeStatus.value}'` });
            listArr.value = records.items;
          }
          getList();

          return {
            listArr,
            status,
            filterList,
            activeStatus,
          };
        },
      });

      app.mount("#app"); //挂载
    </script>
  </body>
</html>
